package com.detech.sbom.business.login.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.detech.sbom.base.annotation.Log;
import com.detech.sbom.base.config.mybatis.PageParam;
import com.detech.sbom.base.constant.CacheConstants;
import com.detech.sbom.base.domain.R;
import com.detech.sbom.base.domain.RE;
import com.detech.sbom.base.enumtype.LogTypeEnum;
import com.detech.sbom.base.enumtype.ModuleEnum;
import com.detech.sbom.base.mybatis.login.entity.TbSysLogininforEntity;
import com.detech.sbom.base.redis.RedisUtils;
import com.detech.sbom.business.login.service.ISysLogininforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统访问记录
 *
 */
@Api(tags = "系统访问记录")
@RestController
@RequestMapping("/logininfor")
public class LogininforController {

    @Autowired
    private ISysLogininforService logininforService;

    @ApiOperation("查询系统访问记录列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name="ipaddr",value="ip地址",dataType="string", paramType = "query", dataTypeClass=String.class,required=false),
        @ApiImplicitParam(name="status",value="状态（0成功 1失败）",dataType="string", paramType = "query", dataTypeClass=String.class,required=false),
        @ApiImplicitParam(name="userName",value="用户账号",dataType="string", paramType = "query", dataTypeClass=String.class,required=false),
        @ApiImplicitParam(name="msg",value="描述",dataType="string", paramType = "query", dataTypeClass=String.class,required=false),
        @ApiImplicitParam(name="accessTime",value="访问时间（yyyy-MM-dd）",dataType="string", dataTypeClass=String.class, paramType = "query",required=false),
        @ApiImplicitParam(name="deviceType",value="登录设备类型",dataType="string", dataTypeClass=String.class, paramType = "query",required=false)
    })
    @SaCheckPermission("system:logininfor:list")
    @Log(model = ModuleEnum.USER_MANAGE,logType = LogTypeEnum.INFO,value = "查询系统访问记录列表", saveDB = false)
    @GetMapping("/list")
    public RE<Page<TbSysLogininforEntity>> list(PageParam pageParam, String ipaddr, String status, String userName, String msg, String accessTime, String deviceType) {
        Page<TbSysLogininforEntity> page = logininforService.selectPageLogininforList(pageParam,ipaddr,status,userName,msg,accessTime,deviceType);
        return R.ok(page);
    }

    @ApiOperation(value = "查询系统访问日志详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value="访问ID",dataType="string", paramType = "query", dataTypeClass=String.class,required=true)
    })
    @SaCheckPermission("system:logininfor:detail")
    @PostMapping(value = "/detail")
    @Log(model = ModuleEnum.USER_MANAGE,logType = LogTypeEnum.INFO,value = "查询系统访问日志详情")
    public RE<TbSysLogininforEntity> detail(String id) {
        TbSysLogininforEntity tbSysLogininfor = logininforService.selectLogininforById(id);
        return R.ok(tbSysLogininfor);
    }

    @ApiOperation(value = "账户解锁")
    @ApiImplicitParams({
        @ApiImplicitParam(name="username",value="帐号",dataType="string", paramType = "query", dataTypeClass=String.class,required=true)
    })
    @SaCheckPermission("system:logininfor:unlock")
    @Log(model = ModuleEnum.USER_MANAGE,logType = LogTypeEnum.WARN,value = "账户解锁")
    @GetMapping("/unlock")
    public RE unlock(String username) {
        String loginName = CacheConstants.PWD_ERR_CNT_KEY + username;
        if (RedisUtils.hasKey(loginName)) {
            RedisUtils.deleteObject(loginName);
        }
        return R.ok();
    }
}
